<?php

class Login extends Controller {
	
	function index()
	{
		if($this->uri->segment(3)!="bestel"){
			$bestel='';
		} else {
			$bestel='/bestel';
		}
		
		$data=init_vars("Aanmelden",$this);

		$data['js'].= script_import(base_url()."lib/login.js");
		
		$this->load->helper('form_helper');
		$data['content'] = "<div class=\"login_form\" style=\"margin-left:auto; margin-right:auto; width:260px;\">\n";
		$data['content'] .= form_open('login/validate_credentials'.$bestel);
		$data['content'] .= form_input('user', 'Gebruikersnaam', 'title="Gebruikersnaam"');
		$data['content'] .= form_password('password', 'Wachtwoord', 'title="Wachtwoord"');
		$data['content'] .= form_checkbox('autologin', 'autologin', TRUE) . "Meld me automatisch aan elk bezoek";
		$data['content'] .= "<table width=\"100%\">\n";
		$data['content'] .= "	<tr>\n";
		$data['content'] .= "		<td align=\"left\">".form_submit('submit', 'Login')."</td>\n";

		$data['content'] .= "		<td align=\"right\">".anchor(base_url().'login/signup'.$bestel,'Registreer',array(
																 'name' => 'registreer',
																 'id' => 'registreer',
																 'value' => 'true'
																 ))."</td>\n";
		$data['content'] .= "	</tr>\n";
		$data['content'] .= "</table>\n";
		$data['content'] .= "</div>\n";
		
		if($this->uri->segment(3)!="bestel"){
			$this->load->view('single',$data);
		} else {
			$data['aanmelden']=true;
			$data['js'].= script_import(base_url()."lib/winkelkar.js");
			$this->load->view('bestel',$data);
		}
	}
	
	function validate_credentials()
	{	
		if($this->uri->segment(3)!="bestel"){
			$bestel='';
		} else {
			$bestel='/bestel';
		}
		
		$this->load->model('membership_model');
		$query = $this->membership_model->validate();
		
		if($query) // if the user's credentials validated...
		{
			$data = array(
				'user' => $this->input->post('user'),
				'is_logged_in' => true
			);
			$this->session->set_userdata($data);
			if($this->uri->segment(3)!="bestel"){
				redirect('jas');
			} else {
				redirect('bestel/controle_adres');
			}
		}
		else // incorrect username or password
		{
			$this->index();
		}
	}	
	
	function signup()
	{
		$this->load->helper('form_helper');
		$this->load->model('membership_model');
		
		if($this->uri->segment(3)!="bestel"){
			$bestel='';
		} else {
			$bestel='/bestel';
		}
		
		$frm_width= 420 ;//260
		
		$data=init_vars("Aanmelden",$this);
		
		$data['js'] = script_import(base_url()."lib/registreer.js");
		$data['js'].= script_import(base_url()."lib/jquery/jquery.ui.js");
		$data['js'].= css_import("http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css");
		
		$data['content'] = validation_errors();
		
		$attributes = array('name' => 'create_member_frm', 'id' => 'create_member_frm');
		$data['content'] .= form_open('login/create_member'.$bestel,$attributes);
		
		$data['content'] .= "<div class=\"login_form\" style=\"margin-left:auto; margin-right:auto; width:".$frm_width."px; margin-bottom:20px;\">\n";
		$data['content'] .= "<h2>Login</h2>\n";
		
		$data['content'] .= $this->form_input_field('user','Gebruikersnaam');
		$data['content'] .= $this->form_password_field('password','Wachtwoord');
		$data['content'] .= $this->form_password_field('retypepassword','Wachtwoord');
		
		$data['content'] .= "</div>\n";
		
		$data['content'] .= "<div class=\"login_form\" style=\"margin-left:auto; margin-right:auto; width:".$frm_width."px;margin-bottom:20px;\">\n";
		$data['content'] .= "<h2>Prive gegevens</h2>\n";
		
		$data['content'] .= $this->form_input_field('surname', 'Voornaam');
		$data['content'] .= $this->form_input_field('family_name', 'Achternaam');
		$data['content'] .= $this->form_input_field('street','Straat + nr');
		$data['content'] .= $this->form_input_field('postcode','Postcode');
		$data['content'] .= $this->form_input_field('city','Gemeente');
		$data['content'] .= $this->form_input_field('email','E-mail');
		$data['content'] .= $this->form_input_field('gsm','GSM nummer');
		$data['content'] .= $this->form_input_field('geboortedatum','Geboortedatum');
		
		$data['content'] .= "</div>\n";
		
		$data['content'] .= "<div class=\"login_form\" style=\"margin-left:auto; margin-right:auto; width:".$frm_width."px;margin-bottom:20px;\">\n";
		$data['content'] .= "<h2>Je vereniging(en)</h2>\n";
		
		$records=$this->membership_model->getVerenigingen();
		$i=0;
		$data['content'] .="<table>\n";
		foreach($records->result_array() as $v){
			if($i%2 == 0){
				$data['content'] .="	<tr>\n";
			}
			$data['content'] .="		<td>" .form_checkbox('v'.$v['id'], $v['id'], FALSE) . $v['vereniging'] . "</td>\n";
			if($i%2 == 1){
				$data['content'] .="	</tr>\n";
			}
			$i++;
		}
		$data['content'] .="</table>\n";
		
		$data['content'] .= "<div align=\"right\" width=\"100%\">".form_submit('submit', 'Verzenden')."</div>\n";
		$data['content'] .= "</div>\n";
		if($this->uri->segment(3)!="bestel"){
			$this->load->view('single',$data);
		} else {
			$data['aanmelden']=true;
			$data['js'].= script_import(base_url()."lib/winkelkar.js");
			$this->load->view('bestel',$data);
		}
	}
	
	function create_member()
	{
		$this->load->library('form_validation');
		
		if($this->uri->segment(3)!="bestel"){
			$bestel='';
		} else {
			$bestel='/bestel';
		}
		
		// field name, error message, validation rules
		$this->form_validation->set_rules('user', 'Gebruikersnaam', 'trim|required|min_length[3]|callback__unique');
		$this->form_validation->set_rules('password', 'Wachtwoord', 'trim|required|min_length[4]|max_length[32]');
		$this->form_validation->set_rules('retypepassword', 'Hertyp Wachtwoord', 'trim|required|matches[password]');
		
		$this->form_validation->set_rules('surname', 'Voornaam', 'trim|required');
		$this->form_validation->set_rules('family_name', 'Achternaam', 'trim|required');
		$this->form_validation->set_rules('street', 'Straat', 'trim|required');
		$this->form_validation->set_rules('postcode', 'Postcode', 'trim|required');
		$this->form_validation->set_rules('city', 'Straat', 'trim|required');
		$this->form_validation->set_rules('email', 'E-mail', 'trim|required|valid_email');
		$this->form_validation->set_rules('gsm', 'GSM Nummer', 'trim|required|min_length[10]|max_length[12]');
		$this->form_validation->set_rules('geboortedatum', 'Geboortedatum', 'callback__datecheck');

		
		
		if($this->form_validation->run() == FALSE)
		{
			$this->signup();
		}
		
		else
		{
			$this->load->model('membership_model');
			
			if($query = $this->membership_model->create_member())
			{
				$data=init_vars("Registreer",$this);
				$data['content'] = 'Registratie succesvol';
				if($this->uri->segment(3)!="bestel"){
					$this->load->view('single',$data);
				} else {
					$data['aanmelden']=true;
					$data['js'].= script_import(base_url()."lib/winkelkar.js");
					$data['js'] .="<script language=\"javascript\">\n";
					$data['js'] .="<!--\n";
					$data['js'] .="	function redirect(){\n";
					$data['js'] .="		window.open(base_url+'login/index/bestel','_top');\n";
					$data['js'] .="	}\n";
					$data['js'] .="	setTimeout(\"redirect()\",2500);\n";
                    $data['js'] .="-->\n";
                    $data['js'] .="</script>\n";
					$this->load->view('bestel',$data);
				}
			}
			else
			{
				$this->signup();
			}
		}
		
	}
	
	function logout()
	{
		$this->session->sess_destroy();
		$this->index();
	}

	function _datecheck($str)
	{
		$date=explode("-",$str);
		if (count($date)==3 && strlen($date[0])==2 && strlen($date[1])==2 && strlen($date[2])==4)
		{
			return TRUE;
		}
		else
		{
			$this->form_validation->set_message('_datecheck', 'Het veld %s moet in de juiste vorm zijn: dd-mm-jjjj (vb: 06-06-2006)');
			return FALSE;
		}
	}
	function _unique($str)
	{
		$this->load->model('membership_model');
		
		if (!$this->membership_model->memberExists($str))
		{
			return TRUE;
		}
		else
		{
			$this->form_validation->set_message('_unique', 'Er is al een gebruiker met deze naam');
			return FALSE;
		}
	}
	
	function save_userdata()
	{
		$user=$this->input->post('user');
		$field=$this->input->post('field');
		$value=$this->input->post('value');
		
		$this->load->model('membership_model');
		if($user!="" && $field!="" && $value!=""){
			if($this->membership_model->updateField($user,$field,$value)){
				$data['antw']="succes!";
			} else {
				$data['antw']="failed!";
			}
		} else {
			$data['antw']="failed! -> error args";	
		}
		$this->load->view('ajax',$data);
	}
	
	function signup2(){
		$data=init_vars("Aanmelden",$this);
		$data['js'].= script_import(base_url()."application/eid/be_belgium_eid.js");
		$data['js'].= script_import(base_url()."lib/eidFile.js");
		
		$this->load->view('single',$data);
	}
	
	private function form_input_field($name,$title){
		$val=(set_value($name)!="")?set_value($name):$title;
		return form_input(array( 'name' => $name,
								 'id' => $name,
								 'value' => $val,
								 'title' => $title)) ."\n";
	}
	
	private function form_password_field($name,$title){
		$val=(set_value($name)!="")?set_value($name):$title;
		return form_password(array( 'name' => $name,
								 	'id' => $name,
								 	'value' => $val,
								 	'title' => $title)) ."\n";
	}
}